package co.fitstart.admin.web.controller.courseware;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.courseware.movement.MovementCategory;
import co.fitstart.entity.courseware.movement.MovementEquipment;


/**
 * 
 * @author Wy
 *
 */

@Controller
@RequestMapping("/movement-attributes")
@MenuSetting("menu-course")
public class MovementAttributeController {
	
	private static final Logger logger = LoggerFactory.getLogger(MovementAttributeController.class);
	
	
	@Autowired
    @Qualifier("dataService")
    private DataService<MovementEquipment> movementEquipmentService;
	
	@Autowired
    @Qualifier("dataService")
    private DataService<MovementCategory> movementCategoryService;
	
	/**
	 * list actions
	 * 
	 * @param page
	 * @return
	 */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-movement-attribute")
    public ModelAndView list() {
    	
    	ModelAndView modelAndView = new ModelAndView("courseware/movement/attribute/attribuate-index");
    	
    	List<MovementCategory> movementCategories = movementCategoryService.listAll(MovementCategory.class);
    	List<MovementEquipment> MovementEquipments= movementEquipmentService.listAll(MovementEquipment.class);
    	
    	modelAndView.addObject("movementCategories", movementCategories);
    	modelAndView.addObject("MovementEquipments", MovementEquipments);
               
        return modelAndView;
    }
    
    /**
     * view create the category
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "/category-create", method = RequestMethod.GET)
    public ModelAndView categoryCreate() {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/attribute/category-create");
               
        return modelAndView;
    }
    
    /**
     * onCreate the Category
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST , params="type=category")
    public @ResponseBody String onCategoryCreate(MovementCategory category) {
        
        Result result = new Result();
        
        try {
                movementCategoryService.save(category);
                result.success().message("新添类别成功！");
            
             } catch(Exception e) {
            
              logger.error("#onCategoryCreate() - ", e);
            
              result.fail().message("新添类别失败！");
            }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * view create the equipment
     * 
     * @param type
     * @return
     */
    @RequestMapping(value = "/equipment-create", method = RequestMethod.GET)
    public ModelAndView equipmentCreate() {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/attribute/equipment-create");
               
        return modelAndView;
    }
    
    /**
     * onCreate the Category
     * 
     * @param equipment
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST ,params="type=equipment")
    public @ResponseBody String onEquipmentCreate(MovementEquipment equipment) {
        
        Result result = new Result();
        
        try {
    		movementEquipmentService.save(equipment);
    		result.success().message("新添器材成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEquipmentCreate() - ", e);
            
            result.fail().message("新添器材失败！");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     *view edit the category
     * 
     * @return
     */
    @RequestMapping(value = "/{attributeId}/category-edit", method = RequestMethod.GET)
    public ModelAndView categoryEdit(@PathVariable("attributeId") Long attributeId ) {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/attribute/category-edit");
        
    	MovementCategory movementCategory=movementCategoryService.load(MovementCategory.class, attributeId);
    	
    	modelAndView.addObject("movementCategory", movementCategory);
        
    	return modelAndView;
    }
    
    /**
     * on edit the category
     * 
     * @param movementCategory
     * @return
     */
    @RequestMapping(value = "/{attributeId}", method = RequestMethod.PATCH , params="type=category")
    public @ResponseBody String onCategoryEdit(@Preloading("attributeId") MovementCategory movementCategory ){
        
        Result result = new Result();
        
        try {
                movementCategoryService.update(movementCategory);
                
                result.success().message("修改类别成功！");
            }
            
        catch(Exception e) {
            
            logger.error("#onCategoryEdit() - ", e);
            
            result.fail().message("修改类别失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    
	/**
	 *view edit the equipment
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{attributeId}/equipment-edit", method = RequestMethod.GET)
	public ModelAndView equipmentEdit(@PathVariable("attributeId") Long attributeId ) {
	    
	    ModelAndView modelAndView = new ModelAndView("courseware/movement/attribute/equipment-edit");
	    
		MovementEquipment movementEquipment = movementEquipmentService.load(MovementEquipment.class, attributeId);
		
		modelAndView.addObject("movementEquipment", movementEquipment);
	    
		return modelAndView;
	}
    
	/**
	 * on edit the equipment
	 * 
	 * @param movementEquipment
	 * @return
	 */
	@RequestMapping(value = "/{attributeId}", method = RequestMethod.PATCH ,params="type=equipment")
	public @ResponseBody String onEquipmentEdit(@Preloading("attributeId") MovementEquipment movementEquipment 
			                           ) {
	    Result result = new Result();
	    try {
	    	
	    		movementEquipmentService.update(movementEquipment);
	    		result.success().message("修改器材成功！");
	        
	    } catch(Exception e) {
	        
	        logger.error("#onEquipmentEdit() - ", e);
	        
	        result.fail().message("修改器材失败！");
	        
	    }
	    
	    return JsonUtils.toJsonString(result);
	}
    
    /**
     * View  delete category
     * 
     * @param attributeId
     * @return
     */
    @RequestMapping(value = "/{attributeId}/category-delete", method = RequestMethod.GET)
    public ModelAndView categoryDelete(@PathVariable("attributeId") Long attributeId) {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/attribute/category-delete");
        
        modelAndView.addObject("attributeId", attributeId);
    		
        return modelAndView;
    }
    
    /**
     * Do delete category 
     * 
     * @param attributeId
     * @return
     */
    @RequestMapping(value = "/{attributeId}", method = RequestMethod.DELETE , params="type=category")
    public @ResponseBody String onCategoryDelete(@RequestParam ("attributeId") Long attributeId) {
        
        Result result = new Result();
        
        try {
            MovementCategory movementCategory =  movementCategoryService.load(MovementCategory.class, attributeId);
            
            movementCategoryService.update(movementCategory.disable());
            
            
            result.success().message("删除动作类别成功！");
            
        } catch(Exception e) {
            
            logger.error("#onCategoryDelete() - ", e);
            
            result.fail().message("删除动作动作类别失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }
    
    /**
     * view the equipment delete
     * 
     * @param attributeId
     * @return
     */
    @RequestMapping(value = "/{attributeId}/equipment-delete", method = RequestMethod.GET)
    public ModelAndView equipmentDelete(@PathVariable("attributeId") Long attributeId) {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/attribute/equipment-delete");
        
        modelAndView.addObject("attributeId", attributeId);
    		
        return modelAndView;
    }
    
    /**
     * Do delete equipment 
     * 
     * @param attributeId
     * @return
     */
    @RequestMapping(value = "/{attributeId}", method = RequestMethod.DELETE , params="type=equipment")
    public @ResponseBody String onEquipmentDelete(@RequestParam ("attributeId") Long attributeId) {
        
        Result result = new Result();
        
        try {
        	
        	MovementEquipment movementEquipment = movementEquipmentService.load(MovementEquipment.class, attributeId);
        	
    		movementEquipmentService.update(movementEquipment.disable());
    		
    		result.success().message("删除器材成功！");
        
        } catch(Exception e) {
            
            logger.error("#onEquipmentDelete() - ", e);
            
            result.fail().message("删除器材失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }
    
}
